/*
 * Copyright © 2018 www.noark.xyz All Rights Reserved.
 *
 * 感谢您选择Noark框架，希望我们的努力能为您提供一个简单、易用、稳定的服务器端框架 ！
 * 除非符合Noark许可协议，否则不得使用该文件，您可以下载许可协议文件：
 *
 *        http://www.noark.xyz/LICENSE
 *
 * 1.未经许可，任何公司及个人不得以任何方式或理由对本框架进行修改、使用和传播;
 * 2.禁止在本项目或任何子项目的基础上发展任何派生版本、修改版本或第三方版本;
 * 3.无论你对源代码做出任何修改和改进，版权都归Noark研发团队所有，我们保留所有权利;
 * 4.凡侵犯Noark版权等知识产权的，必依法追究其法律责任，特此郑重法律声明！
 */
package xyz.noark.codec.simple;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.util.JDKUtils;
import org.apache.fury.Fury;
import org.apache.fury.config.Language;
import org.apache.fury.logging.LoggerFactory;
import xyz.noark.codec.Codec;
import xyz.noark.codec.encoder.ObjectEncoder;
import xyz.noark.codec.simple.data.SimpleData;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 小流氓[176543888@qq.com]
 */
public class SimpleDataTest {
    public static SimpleData data;

    static ConcurrentHashMap<Class<?>, ObjectEncoder<?>> cahceEncoders = new ConcurrentHashMap<>();

    static {
        data = new SimpleData();
        data.setId(1);
        data.setTemplateId(2);
        data.setName("哈哈哈哈哈哈");
        data.setFlag(false);
        cahceEncoders.put(SimpleData.class, new ObjectEncoderImplSimpleDataGen());
    }

    public void testEncode() throws IOException {
        LoggerFactory.disableLogging();
        byte[] array = encode(data);
        System.out.println(Arrays.toString(array));
        System.out.println(array.length + "\t" + new String(array));

        byte[] jsonb = JSONB.toBytes(data);
        System.out.println(jsonb.length + "\t" + new String(jsonb));


        {
            Fury fury = Fury.builder().withLanguage(Language.JAVA)
                    .withRefTracking(true).requireClassRegistration(false).build();
            byte[] result = fury.serialize(data);
            SimpleData simpleData = (SimpleData) fury.deserialize(result);


            System.out.println(result.length + "\t" + new String(result));

            System.out.println(JSON.toJSONString(simpleData));
        }

        String str = "abc";
        System.out.println(Arrays.toString(str.getBytes(StandardCharsets.UTF_8)));
    }


    static byte[] buffer = new byte[10240];

    public static <T> byte[] encode(T data) throws IOException {
        return Codec.encode(data);
    }

    public static void main(String[] args) {
        Codec.encode(data);
    }

    public void testX() {
        String value = "123";
        System.out.println("Lambda:" + Arrays.toString(JDKUtils.STRING_VALUE.apply(value)));
        System.out.println("getBytes:" + Arrays.toString(value.getBytes()));
        System.out.println("getBytes8:" + Arrays.toString(value.getBytes(StandardCharsets.UTF_8)));
        System.out.println("getBytes16:" + Arrays.toString(value.getBytes(StandardCharsets.UTF_16)));
    }
}